<?php
namespace App\Server;

use EasySwoole\EasySwoole\Task\TaskManager;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Socket\AbstractInterface\Controller;

use App\Storage\OnlineUser;
use App\Storage\OnlineClient;

class Server extends Controller
{
    /**
     *  数组转xml格式字符串
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 18:42:11
     * @param array $arr
     * @return void
     */
    protected function ArrToXml($arr)
    {
        if (!is_array($arr) || count($arr) == 0) {
            return '';
        }
        $xml =  '<?xml version="1.0"?>';
        $xml .= '<Message>';
        foreach ($arr as $key=>$val) {
            $xml .= '<' . $key . '>';
            $xml .= $val;
            $xml .= '</' . $key . '>';
        }
        $xml .= '</Message>';
        return $xml;
    }

    /**
     * 获取UTC格式的时间
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-22 19:01:06
     * @return string $Time
     */
    protected function Time()
    {
        $timestamp = time();
        $Time = date('Y-m-d H:i:s', $timestamp);
        $Time = explode(' ', $Time);
        $Time[0] .= '-T';
        $Time[1] .= 'Z';
        $Time = $Time[0] . $Time[1];
        return $Time;
    }

    /**
     * 关闭未知用户|终端的连接
     *
     * @Author LYC 17750800820@163.com
     * @DateTime 2020-04-24 11:53:24
     * @return void
     */
    public function destroy()
    {        
        $args = $this->caller()->getArgs();
        $client = $this->caller()->getClient();
        $server = ServerManager::getInstance()->getSwooleServer();
        $server->close($client->getFd());
        OnlineUser::getInstance()->delete($client->getFd());
        OnlineClient::getInstance()->delete($client->getFd());
    }
}
